package com.hb.trans.mvp.ui.activity


import android.annotation.SuppressLint
import android.os.Bundle
import android.view.MenuItem
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.basiclib.base.BaseActivity
import com.basiclib.utils.ImmersionBar
import com.hb.trans.R
import com.hb.trans.bean.Period
import com.hb.trans.global.ACTIVITY_TRANS_RECORD_EVALUATE
import com.hb.trans.global.DataStore
import com.hb.trans.mvp.contract.EvaluateCoachContract
import com.hb.trans.mvp.presenter.EvaluateCoachPresenter
import com.jakewharton.rxbinding2.view.clicks
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_evaluate_coach.*
import javax.inject.Inject

/**
 * 评价
 */
@Route(path = ACTIVITY_TRANS_RECORD_EVALUATE)
class EvaluateCoachActivity : BaseActivity(), EvaluateCoachContract.View {


    @Inject
    lateinit var mPresenter: EvaluateCoachPresenter
    @JvmField
    @Autowired
    var period: Period? = null

    override fun getContentViewResId(): Int = R.layout.activity_evaluate_coach

    override fun onActivityInject() {
        AndroidInjection.inject(this)
    }

    override fun attachView(savedInstanceState: Bundle?) {
        ARouter.getInstance().inject(this)
        mPresenter.attachView(this)
        ImmersionBar.with(this)
            .statusBarColor(R.color.colorPrimary)
            .init()
    }

    override fun getPresenter(): EvaluateCoachPresenter? = mPresenter

    override fun initView() {
        setSupportActionBar(evaluateToolbar.toolbar)
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        supportActionBar!!.setHomeAsUpIndicator(R.mipmap.ic_left_arrow)

    }
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> onBackPressed()
        }
        return true
    }


    @SuppressLint("CheckResult")
    override fun setListener() {
//        evaluateToolbar.backClick()?.subscribe { finish() }
        btnCommitEvaluate.clicks().subscribe {
            val content = etEvaluateContent.text.toString().trim()
            when {
                content.isNullOrBlank() -> showShortToast("请输入评价内容")
                content.length > 100 -> showShortToast("评价内容字数不能超过100字")
                else -> period?.let { p ->
                    val student = DataStore.getInstance().getStudentInfo()
                    student?.let { stu ->
                        mPresenter.commitEvaluate(
                            stu.stunum,
                            p.inscode,
                            p.coachName,
                            p.coanum,
                            content,
                            p.periodId
                        )
                    }
                } ?: showShortToast("数据异常，请返回上一页")
            }
        }
    }

    override fun commitSucess(message: String?) {
        finish()
    }

    override fun initData() {

    }

    override fun onDestroy() {
        super.onDestroy()
        ImmersionBar.with(this).destroy()
    }
}
